#!/bin/bash -e

SCRIPT_DIR=$(cd `dirname $0`; pwd)
source $SCRIPT_DIR/common.sh

CMAKE_FOLDER=$(cd $ANDROID_HOME/cmake && ls -1 | sort -r | head -1)
PATH=$TOOLCHAIN_DIR/bin:$ANDROID_HOME/cmake/$CMAKE_FOLDER/bin/:$PATH

rm -rf $TARGETDIR/webkit/$CROSS_COMPILE_PLATFORM-${FLAVOR}
rm -rf $TARGETDIR/webkit/WebKitBuild
cd $TARGETDIR/webkit/Tools/Scripts

CMAKE_CXX_FLAGS=" \
$SWITCH_JSC_CFLAGS_COMPAT \
$JSC_CFLAGS \
$PLATFORM_CFLAGS \
-I$TARGETDIR/icu/source/i18n \
-I$TOOLCHAIN_DIR/sysroot/usr/include \
"

CMAKE_LD_FLAGS=" \
-latomic \
-lm \
-lc++_shared \
$JSC_LDFLAGS \
$PLATFORM_LDFLAGS \
"

export AR=$CROSS_COMPILE_PLATFORM-ar
export AS=$CROSS_COMPILE_PLATFORM-clang
export CC=$CROSS_COMPILE_PLATFORM-clang
export CXX=$CROSS_COMPILE_PLATFORM-clang++
export LD=$CROSS_COMPILE_PLATFORM-ld
export STRIP=$CROSS_COMPILE_PLATFORM-strip

ARCH_NAME_PLATFORM_arm="arm"
ARCH_NAME_PLATFORM_arm64="aarch64"
ARCH_NAME_PLATFORM_x86="i686"
ARCH_NAME_PLATFORM_x86_64="x86_64"
var="ARCH_NAME_PLATFORM_$JSC_ARCH"
export ARCH_NAME=${!var}

$TARGETDIR/webkit/Tools/Scripts/build-webkit \
  --jsc-only \
  --release \
  --jit \
  "$SWITCH_BUILD_WEBKIT_OPTIONS_INTL" \
  --no-webassembly \
  --no-xslt \
  --no-netscape-plugin-api \
  --no-tools \
  --cmakeargs="-DCMAKE_SYSTEM_NAME=Android \
  $SWITCH_BUILD_WEBKIT_CMAKE_ARGS_COMPAT \
  -DCMAKE_SYSTEM_VERSION=$ANDROID_API \
  -DCMAKE_SYSTEM_PROCESSOR=$ARCH_NAME \
  -DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=$TOOLCHAIN_DIR \
  -DWEBKIT_LIBRARIES_INCLUDE_DIR=$TARGETDIR/icu/source/common \
  -DWEBKIT_LIBRARIES_LINK_DIR=$TARGETDIR/icu/${CROSS_COMPILE_PLATFORM}-${FLAVOR}/lib \
  -DCMAKE_C_COMPILER=$CROSS_COMPILE_PLATFORM-clang \
  -DCMAKE_CXX_COMPILER=$CROSS_COMPILE_PLATFORM-clang \
  -DCMAKE_SYSROOT=$TOOLCHAIN_DIR/sysroot \
  -DCMAKE_SYSROOT_COMPILE=$TOOLCHAIN_DIR/sysroot \
  -DCMAKE_CXX_FLAGS='${CMAKE_CXX_FLAGS} $COMMON_CXXFLAGS $CMAKE_CXX_FLAGS' \
  -DCMAKE_C_FLAGS='${CMAKE_C_FLAGS} $CMAKE_CXX_FLAGS' \
  -DCMAKE_SHARED_LINKER_FLAGS='${CMAKE_SHARED_LINKER_FLAGS} $CMAKE_LD_FLAGS' \
  -DCMAKE_EXE_LINKER_FLAGS='${CMAKE_MODULE_LINKER_FLAGS} $CMAKE_LD_FLAGS' \
  -DENABLE_API_TESTS=0 \
  -DCMAKE_VERBOSE_MAKEFILE=on \
  -DJSC_VERSION=\"${JSC_VERSION}\" \
  "

cp $TARGETDIR/webkit/WebKitBuild/Release/lib/libjsc.so $INSTALL_DIR
mv $TARGETDIR/webkit/WebKitBuild $TARGETDIR/webkit/${CROSS_COMPILE_PLATFORM}-${FLAVOR}
cp $TOOLCHAIN_LINK_DIR/libc++_shared.so $INSTALL_DIR
